package cn.ichiva.common.concurrent;

import java.util.concurrent.*;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 线程池创建工具
 */
public class ExecutorsPlus {
    private static final AtomicInteger blockFixedThreadIndex = new AtomicInteger();

    /**
     * 固定线程阻塞型线程池
     *
     * 拒绝策略为让提交线程执行任务
     * @param nThreads
     * @return
     */
    public static ThreadPoolExecutor newBlockFixedThreadPool(int nThreads){
        return new ThreadPoolExecutor(
                nThreads,
                nThreads,
                0,
                TimeUnit.MILLISECONDS,
                new ArrayBlockingQueue<>(nThreads),
                r -> new Thread(r,"T-" + blockFixedThreadIndex.incrementAndGet()),
                new ThreadPoolExecutor.CallerRunsPolicy());
    }
}
